package com.javaweb.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.admin.entity.CodeRules;
import com.javaweb.admin.mapper.CodeRulesMapper;
import com.javaweb.admin.query.CodeRulesQuery;
import com.javaweb.admin.service.ICodeRulesService;
import com.javaweb.admin.vo.coderules.CodeRulesInfoVo;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.system.utils.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
  * <p>
  *  服务类实现
  * </p>
  *
  * @author 鲲鹏
  * @since 2020-12-23
  */
@Service
public class CodeRulesServiceImpl extends BaseServiceImpl<CodeRulesMapper, CodeRules> implements ICodeRulesService {

    @Autowired
    private CodeRulesMapper codeRulesMapper;

    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getList(BaseQuery query) {
        CodeRulesQuery codeRulesQuery = (CodeRulesQuery) query;
        // 查询条件
        QueryWrapper<CodeRules> queryWrapper = new QueryWrapper<>();

        //
        if (!StringUtils.isEmpty(codeRulesQuery.getBz())) {
            queryWrapper.like("bz", codeRulesQuery.getBz());
        }
        //
        if (!StringUtils.isEmpty(codeRulesQuery.getZzcode())) {
            queryWrapper.like("zzcode", codeRulesQuery.getZzcode());
        }
        //queryWrapper.eq("company_id",ShiroUtils.getCompanyId());
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByDesc("id");

        // 获取数据列表
        IPage<CodeRules> page = new Page<>(codeRulesQuery.getPage(), codeRulesQuery.getLimit());
        IPage<CodeRules> pageData = codeRulesMapper.selectPage(page, queryWrapper);
        pageData.convert(x -> {
            return x;
        });
        return JsonResult.success(pageData);
    }

    /**
     * 获取详情Vo
     *
     * @param id 记录ID
     * @return
     */
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public Object getInfo(Serializable id) {
        CodeRules entity = (CodeRules) super.getInfo(id);
        // 返回视图Vo
        CodeRulesInfoVo codeRulesInfoVo = new CodeRulesInfoVo();
        // 拷贝属性
        BeanUtils.copyProperties(entity, codeRulesInfoVo);
        return codeRulesInfoVo;
    }

    /**
     * 添加、更新记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult edit(CodeRules entity) {
        if (entity.getId() != null && entity.getId() > 0) {
            try{
            entity.setUpdateUser(ShiroUtils.getUserId());
            entity.setUpdateTime(DateUtils.now());
            return super.edit(entity);
        } catch(Exception ex)
        {
            if(ex.getCause().getMessage().indexOf("唯一索引")!=1) {
                return JsonResult.error("编码规则：" + entity.getZzcode() + "修改失败：编码规则不能重复！" );
            }else {
                return JsonResult.error("编码规则：" + entity.getZzcode() + "修改失败。" + ex.getCause().getMessage());
            }
        }
        } else {
            try{
            entity.setCreateUser(ShiroUtils.getUserId());
            entity.setCreateTime(DateUtils.now());
            return super.edit(entity);
            } catch(Exception ex)
            {
                if(ex.getCause().getMessage().indexOf("唯一索引")!=1) {
                    return JsonResult.error("编码规则：" + entity.getZzcode() + "添加失败：编码规则不能重复！" );
                }else {
                    return JsonResult.error("编码规则：" + entity.getZzcode() + "添加失败。" + ex.getCause().getMessage());
                }
            }
        }
    }

    /**
     * 删除记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult delete(CodeRules entity) {
        entity.setUpdateUser(1);
        entity.setUpdateTime(DateUtils.now());
        entity.setMark(0);
        return super.delete(entity);
    }
}